<%@page import="com.nxu.entity.UserBean"%>
<%@page import="com.nxu.entity.BookBean"%>
<%@page import="java.util.List"%>
<%@page import="com.nxu.biz.impl.BooksBizImpl"%>
<%@page import="com.nxu.biz.BooksBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%-- 权限检查：允许普通用户(userType=2)和未登录用户访问 --%>
<%
    UserBean user = (UserBean)session.getAttribute("user");
    // 如果用户已登录且是管理员或商家，重定向到对应页面
    if(user != null) {
        if(user.getUserType() == 1) {
            response.sendRedirect("manage.jsp");
            return;
        } else if(user.getUserType() == 3) {
            response.sendRedirect("merchant.jsp");
            return;
        }
    }
    
    // 获取错误消息
    String error = request.getParameter("error");
    String errorMessage = "";
    if("book_unavailable".equals(error)) {
        errorMessage = "该商品已下架，无法查看";
    }
%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>书香阁 - 商品展示</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), 
                        url('https://images.unsplash.com/photo-1507842217343-583bb7270b66?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center fixed;
            background-size: cover;
            color: #333;
        }
        
        .container {
            max-width: 1200px;
            margin: 20px auto;
            background: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            backdrop-filter: blur(5px);
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .user-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .user-status {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: #8d6e63;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }
        
        .user-name {
            font-weight: bold;
            color: #5a3921;
        }
        
        .logout-btn {
            padding: 8px 15px;
            background-color: #8d6e63;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .logout-btn:hover {
            background-color: #6d4c41;
            transform: translateY(-2px);
        }
        
        .error-message {
            color: #d32f2f;
            text-align: center;
            margin-bottom: 20px;
            padding: 10px;
            background-color: #ffebee;
            border-radius: 4px;
            border-left: 4px solid #d32f2f;
        }
        
        .book-count {
            color: #5a3921;
            margin-bottom: 20px;
            font-size: 18px;
            text-align: left;
            font-weight: bold;
        }
        
        .book-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            gap: 25px;
        }
        
        .book-item {
            width: calc(20% - 25px);
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
            transition: all 0.3s;
        }
        
        .book-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
        }
        
        .book-item img {
            max-width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 5px;
            margin-bottom: 10px;
            border: 1px solid #eee;
        }
        
        .book-item a {
            text-decoration: none;
            color: #333;
        }
        
        .book-title {
            font-size: 16px;
            font-weight: bold;
            margin: 10px 0;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            color: #5a3921;
        }
        
        .book-price {
            font-size: 18px;
            color: #e74c3c;
            font-weight: bold;
        }
        
        .status-badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: bold;
            margin-top: 5px;
        }
        
        .status-onshelf {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-offshelf {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        .login-prompt {
            text-align: center;
            margin-top: 20px;
            color: #5a3921;
        }
        
        .login-prompt a {
            color: #8d6e63;
            text-decoration: none;
            font-weight: bold;
        }
        
        .login-prompt a:hover {
            text-decoration: underline;
        }
        
        @media (max-width: 1200px) {
            .book-item {
                width: calc(25% - 25px);
            }
        }
        
        @media (max-width: 900px) {
            .book-item {
                width: calc(33.33% - 25px);
            }
        }
        
        @media (max-width: 600px) {
            .book-item {
                width: calc(50% - 25px);
            }
        }
        
        @media (max-width: 400px) {
            .book-item {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <% if(user != null) { %>
        <div class="user-info">
            <div class="user-status">
                <div class="user-avatar"><%= user.getUsername().substring(0, 1).toUpperCase() %></div>
                <span class="user-name">欢迎，<%= user.getUsername() %></span>
            </div>
            <button class="logout-btn" onclick="logout()">退出登录</button>
        </div>
        <% } %>
        
        <% if(!errorMessage.isEmpty()) { %>
            <div class="error-message"><%= errorMessage %></div>
        <% } %>
        
        <%
        BooksBiz biz = new BooksBizImpl();
        List<BookBean> books = biz.getAllBooks();
        %>
        
        <div class="book-count">共查询到 <%=books.size()%> 本书</div>
        
        <div class="book-list">
            <%
            for (BookBean book : books) {
                // 只显示上架商品
                if(book.isActive()) {
            %>
            <div class="book-item">
                <a href="detail.jsp?isbn=<%=book.getIsbn()%>">
                    <img src="images/<%=book.getImagefile()%>" alt="<%=book.getTitle()%>" 
                         onerror="this.src='images/default-book.jpg'">
                    <div class="book-title" title="<%=book.getTitle()%>"><%=book.getTitle()%></div>
                    <div class="book-price">￥<%=book.getPrice()%></div>
                    <div class="status-badge status-onshelf">在售</div>
                </a>
            </div>
            <%
                }
            }
            %>
        </div>
        
        <% if(user == null) { %>
        <div class="login-prompt">
            您还未登录，<a href="login.jsp">请登录</a>以获取更多功能
        </div>
        <% } %>
    </div>
    
    <script>
        function logout() {
            if(confirm('确定要退出登录吗？')) {
                window.location.href = 'login.jsp';
            }
        }
    </script>
</body>
</html>